package com.eastfair.matching.service.impl;

import com.eastfair.auth.api.AuthServiceFeign;
import com.eastfair.auth.dto.UserRoleDto;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.matching.service.AuthMatchUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户角色服务.
 *
 * @author dqq
 * @date 2022 /7/25
 */
@Service
@Slf4j
public class AuthMatchUserRoleServiceImpl implements AuthMatchUserRoleService {

    @Resource
    private AuthServiceFeign authServiceFeign;

    @Override
    public R<?> addUserRole(UserRoleDto userRoleDto) {
        R<?> r = authServiceFeign.addUserRole(userRoleDto);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r;
    }

    @Override
    public R<?> addUserRole(Long userId, Long roleId) {
        UserRoleDto userRoleDto = new UserRoleDto();
        List<Long> userIds = new ArrayList<>();
        userIds.add(userId);
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(roleId);
        userRoleDto.setUserIds(userIds);
        userRoleDto.setRoleIds(roleIds);
        return addUserRole(userRoleDto);
    }

    @Override
    public R<?> updUserRole(UserRoleDto userRoleDto) {
        R<?> r = authServiceFeign.updUserRole(userRoleDto);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r;
    }

    @Override
    public R<?> removeOrgRole(UserRoleDto userRoleDto) {
        R<?> r = authServiceFeign.removeOrgRole(userRoleDto);
        if (!r.getIsSuccess()) {
            throw BizException.wrap(r.getCode(), r.getMsg());
        }
        return r;
    }

    @Override
    public R<?> removeUserRole(List<Long> userIds, Long roleId) {
        UserRoleDto userRoleDto = new UserRoleDto();
        userRoleDto.setUserIds(userIds);
        List<Long> roleIds = new ArrayList<>(16);
        roleIds.add(roleId);
        userRoleDto.setRoleIds(roleIds);
        return removeOrgRole(userRoleDto);
    }
}